//-----------------------------------------------------------------------
// <copyright file="Update.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
// </copyright>
// <author>Andy Reeves</author>
// <email>andyr</email>
// <date>2004-03-23</date>
// <summary>Updates the specified ComponentServices Application.</summary>
//-----------------------------------------------------------------------

namespace Microsoft.Sdc.Tasks.ComponentServices.Application
{
    using System;
    using Microsoft.Sdc.Tasks.Configuration.ComponentServices;
    using Microsoft.Build.Framework;

    /// <summary>
    /// Updates the specified ComponentServices Application.
    /// </summary>
    /// <remarks>
    /// <code><![CDATA[<ComponentServices.Application.Update ApplicationName="applicationName" Identity="identity" Password="password" ShutdownAfter="shutDownAfter" ActivationString="activationString" EnforceAccessChecks="enforceAccessChecks" RunForever="runForever" />]]></code>
    /// <para>where:</para>
    /// <para><i>applicationName</i></para>
    /// <para>The name of the COM+ application. This application will be created if it does not exist.</para>
    /// <para><i>identity</i></para>
    /// <para>Sets the server process identity for the application. Specify a valid user account or "Interactive User" to have the application assume the identity of the current logged-on user. This is not enabled for library applications, which run in the client process.</para>
    /// <para><i>password</i></para>
    /// <para>Sets the password used by the server process to log on under the identity. Password should be set at the same time as Identity, because the password and identity are validated before being saved. If the password and identity get out of sync, the application cannot be launched until they are reset by an administrator.</para>
    /// <para><i>shutDownAfter</i></para>
    /// <para>Sets the delay before shutting down a server process after it becomes idle. Shutdown latency ranges from 0 to 1440 minutes (24 hours). If RunForever is set to True, this property is ignored. ShutdownAfter is not enabled for library (in-process) applications.</para>
    /// <para><i>activationString</i></para>
    /// <para>Set to "Local" to indicate that objects within the application run within a dedicated local server process (server application). Set to "Inproc" to indicate that objects run in their creator's process (library application). Defaults to local</para>
    /// <para><i>enforceAccessChecks</i></para>
    /// <para>Indicates whether access checks are performed for the application when clients make calls into it.</para>
    /// <para><i>runForever</i></para>
    /// <para>Enables a server process to continue if an application is idle. If set to True, the server process does not shut down when left idle. If set to False, the process shuts down according to the value set by the ShutdownAfter property. RunForever is not enabled for library (in-process) applications.</para>
    /// </remarks>
    /// <example>
    /// <code><![CDATA[
    /// <Project>
    ///     <Target Name="Test" >
    ///         <ComponentServices.Application.Update
    ///             ApplicationName="MyApplicationName"
    ///             Identity="Interactive User"
    ///             ActivationString="Local"
    ///             ShutdownAfter="60"
    ///             RunForever="false" />
    ///     </Target>
    /// </Project>
    /// ]]></code>    
    /// </example>
    public class Update : TaskBase
    {
        private string applicationName;
        private string identity;
        private string password;
        private bool enforceAccessChecks;
        private bool runForever;
        private string shutdownAfter;
        private string activationString = String.Empty;

        #region Public Properties

        /// <summary>
        /// Gets or sets the name of the application to be updated.
        /// </summary>
        /// <value>The name of the COM+ application to be updated</value>
        [Required]
        public string ApplicationName
        {
            get { return this.applicationName; }
            set { this.applicationName = value; }
        }

        /// <summary>
        /// Gets or sets the name of the user identity for the COM+ application
        /// </summary>
        /// <value>Sets the server process identity for the application. Specify a valid user account or "Interactive User" to have the application assume the identity of the current logged-on user. This is not enabled for library applications, which run in the client process.</value>
        public string Identity
        {
            get { return this.identity; }
            set { this.identity = value; }
        }

        /// <summary>
        /// Gets or sets the password of the user identity for the COM+ application.
        /// </summary>
        /// <value>Sets the password used by the server process to log on under the identity. Not required if the Identity property is set to "Interactive User".</value>
        public string Password
        {
            get { return this.password; }
            set { this.password = value; }
        }

        /// <summary>
        /// Gets or sets the delay before shutting down a server process after it becomes idle.
        /// </summary>
        /// <value>The delay, in minutes, before the process shutsdown after becoming idle. Shutdown latency ranges from 0 to 1440 minutes (24 hours). If RunForever is set to True, this property is ignored. ShutdownAfter is not enabled for library (in-process) applications.</value>
        public string ShutdownAfter
        {
            get { return this.shutdownAfter; }
            set { this.shutdownAfter = value; }
        }

        /// <summary>
        /// Gets or sets the type of activation for the application.
        /// </summary>
        /// <value>"Local" - local activation indicates that objects within the application run within a dedicated local server process (server application). 
        /// "Inproc" - in-process activation indicates that objects run in their creator's process (library application). 
        /// Defaults to "Local".</value>
        public string ActivationString
        {
            get { return this.activationString; }
            set { this.activationString = value; }
        }

        /// <summary>
        /// Gets or sets a value indicating whether access checks are performed for the application when clients make calls into it.
        /// </summary>
        /// <value>Set to true if access checks are to be performed.</value>
        public bool EnforceAccessChecks
        {
            get { return this.enforceAccessChecks; }
            set { this.enforceAccessChecks = value; }
        }

        /// <summary>
        /// Gets or sets a value indicating whether a server process should continue if an application is idle. 
        /// </summary>
        /// <value>If set to True, the server process does not shut down when left idle. If set to False, the process shuts down according to the value set by the ShutdownAfter property. RunForever is not enabled for library (in-process) applications.</value>
        public bool RunForever
        {
            get { return this.runForever; }
            set { this.runForever = value; }
        }

        #endregion

        /// <summary>
        /// Performs the action of this task.
        /// </summary>
        protected override void InternalExecute()
        {
            Activation activation = Activation.Local;
            if (this.activationString.Length > 0)
            {
                activation = (Activation)Enum.Parse(typeof(Activation), this.activationString, true);
            }

            Utilities.UpdateApplication(this.applicationName, this.identity, this.password, this.enforceAccessChecks, activation, this.runForever, this.shutdownAfter);
        }
    }
}